$(function(){
    function cha() {
        var sele=document.getElementsByName("userImg");
        var op="<option value='0'>---请选择---</option>"
        $.ajax({
            url: "/Role/All",
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                for (let j=0;j<sele.length;j++){
                    sele[j].innerHTML=op;
                    for (var i=0;i<text.length;i++){
                        var option="<option value="+text[i].roleId+">"+text[i].roleName+"</option>";
                        sele[j].innerHTML+=option;
                    }
                }
            }
        })
    }
    cha();
    $("#UserRole").bootstrapTable({
        method:"get",
        //请求头部的设置
        contentType: 'application/json',
        url:"/User/find1",
        //设置交替行样式
        striped: true,
        //初始显示页号
        pageNumber: 1,
        //设置表格高度
        //height: 350,
        //是否显示分页
        pagination: true,
        //设置是服务端分页还是客户端分页
        sidePagination: 'server',
        //每页显示的数量
        pageSize: 3,
        //可选每页显示的数量
        pageList: [3, 5, 6, 10],
        //上传到服务端的参数
        queryParams:function(params){
            return{
                pageNum:(params.offset/params.limit)+1,
                pageSize:params.limit,
                userName:$("#rname").val(),
            }
        },
        columns:[
            {title:'编号',field:'userId',align:'center'},
            {title:'账号',field:'userAccount',align:'center'},
            {title:'密码',field:'userPassword',align:'center'},
            {title:'员工姓名',field:'userName',align:'center'},
            {title:'职位',field:'role.roleName',align:'center'},
            {title:'部门',field:'deptId',align:'center',
            formatter:function (value,row,index) {
                let dept="";
                if(value==1){
                    dept="产品部";
                    return dept;
                }else if(value==2){
                    dept="生产部";
                    return dept;
                }else {
                    dept='仓管部';
                    return dept;
                }
            }},
            {title:'操作',field:'userId',align:'center',
                formatter:function(value,row,index){
                    let but2="<button type='button' class='btn btn-primary' id='stu-delete' onclick='del_user("+value+")'>删除</button>";
                    return but2
                }
            },
        ]
    })
    $("#role-find").click(function(){
        $("#UserRole").bootstrapTable("refresh")
    })
})
function del_user(userId) {
    let user={
        userId:userId,
        userStatus:0
    }
    if(confirm("确定要删除吗")) {
        $.ajax({
            url: "/User/up",
            type: "put",
            data: JSON.stringify(user),
            contentType: "application/json;charset=utf-8",
            success: function (text) {
                $("#UserRole").bootstrapTable("refresh");
            }
        })
    }
}

function yanzheng() {
    if ($("#pwd").val()!=$("#pass").val()){
        $("#txt").html("两次密码不符合，请重新输入");
        $("#pwd").val("");
        $("#pass").val("");
        $("#cao").attr("disabled","true");
    }else {
        $("#cao").removeAttr("disabled");
        $("#txt").html("");
    }
}

function insert_role() {
    let user=getFormData($("#roles"))
    $.ajax({
        url: "/User/add",
        type: "post",
        data: user,
        contentType: "application/json;charset=utf-8",
        success: function (text) {
            $("#UserRole").bootstrapTable("refresh");
        }
    })
}
function yz() {
    let a=false
    var tx=/^[0-9a-zA-Z]*$/g
    if(tx.test($("#zh").val())){
        a=false;
    }else {
        $("#tt").html("只能由英文和数字组成");
        $("#zh").val("");
        a=true;
    }

    $.ajax({
        url: "/User/all",
        type: "get",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (text) {

           for(let i=0;i<text.length;i++){
               if(text[i].userAccount==$("#zh").val()){
                   $("#tt").html("该账号已经存在，请重新输入");
                   $("#zh").val("");
                   a=true;
               }
           }
        }
    })
    if(a==false){
        $("#tt").html("");
    }
}